//admin.js
//获取应用实例
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    shopList:{}
  },
  headImg:function(){
    wx:wx.navigateTo({
      url: 'perfectData/perfectData',
    })
  },
  //推荐店铺数据
  adminShopList:function(){
    let that = this
    var jing = wx.getStorageSync('jing')
    var wei = wx.getStorageSync('wei')
    wx.request({
      url: api.IndexShopListUrl,
      data: {
        'jing': jing,
        'wei': wei,
      },
      method: 'POST',
      success: function (res) {
        that.setData({
          shopList: res.data.data,
        })
      }
    })
   
  },
  withdrawals:function(){
    wx.showModal({
      title: '该功能暂未开放',
      content: '请点击确定',
      showCancel: false
    })
  },
  adminIncome: function () {
    wx.showModal({
      url:'income/income'
    })
  },
  adminExtend: function () {
    wx.showModal({
      title: '该功能暂未开放',
      content: '请点击确定',
      showCancel: false     
    })
  },
  adminVideo: function () {
    wx.showModal({
      title: '该功能暂未开放',
      content: '请点击确定',
      showCancel: false,
    })
  },
  //用户登录成功
  userLogin: function(){
    let that = this
    wx.login({
      success: res => {
        var code1 = res.code
        wx.getUserInfo({
          success: function (res) {
            that.setData({
              userInfo: res.userInfo
            })
            wx.setStorageSync('userInfo', that.data.userInfo)
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            if (this.userInfoReadyCallback) {
              this.userInfoReadyCallback(res)
            }
            var userInfo = res.userInfo
            var ed = res.encryptedData
            var iv = res.iv
            var code = code1
            util.request(api.UserInfoUrl, { encrypte: ed, iv: iv, code: code }, 'POST').then(function (res) {
              console.log(res)
              if (res.data.code === 200) {
                var unionid = res.data.data.unionid
                wx.setStorageSync('unionid', unionid)
              }
            });
          },
          fail: function (res) {
            console.log("获取用户信息失败")
            that.failLogin()
          }
        })
      }
    })
  },
  //用户登录失败
  failLogin:function(){
    let that = this
    wx.openSetting({
      success: function (res) {
        console.log("重新获取用户信息成功")
        that.userLogin()
      },
      fail:function(){
        that.failLogin()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.adminShopList()
    this.userLogin()
  }
  
})
